package com.travelgroup.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travelgroup.constant.RedisConstant;
import com.travelgroup.domain.dto.RouteDTO;
import com.travelgroup.domain.dto.RouteLocationDTO;
import com.travelgroup.domain.entity.RouteLocation;
import com.travelgroup.exception.BusinessException;
import com.travelgroup.mapper.RouteLocationMapper;
import com.travelgroup.service.IRouteLocationService;
import com.travelgroup.utils.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RouteLocationServiceImpl extends ServiceImpl<RouteLocationMapper, RouteLocation>
        implements IRouteLocationService {
    @Resource
    private RedisUtil redisUtil;

    /**
     * 设置路线途径地点
     * @param routeDTO
     * @param id
     */
    @Override
    @Transactional
    public void saveRouteLocation(RouteDTO routeDTO, Long id) {
        List<RouteLocationDTO> routeLocations = routeDTO.getRouteLocations();
        List<RouteLocation> routeLocationList = routeLocations.stream()
                .map(r -> BeanUtil.copyProperties(r, RouteLocation.class))
                .toList();
        routeLocationList.forEach(r -> r.setRouteId(id));
        if (this.saveBatch(routeLocationList)) {
            throw new BusinessException("路线途径地点存储失败");
        }
        redisUtil.removeById(RedisConstant.ROUTE_LOCATION_KEY, id);
    }
}
